<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>websocket 连接测试 ,发送一个登陆包，并返回显示</title>
    <script src="protobuf.js"></script>
</head>
<body>
<div id="output"></div>
<button onclick="connectWebSocket()" value="连接">连接</button>
<script type="text/javascript">


    var LoginRequest;
    var loginRequest;
    var LoginResponse;
    var buffer;
    var arrayBuffer;

    protobuf.load("HallLoginMessage.proto", function (err, root) {
        if (err) throw err;
        LoginResponse = root.lookup("LoginResponse");
    });

    protobuf.load("HallLoginMessage.proto", function (err, root) {
        if (err) throw err;
        LoginRequest = root.lookup("LoginRequest");
        loginRequest = LoginRequest.create({account: "jzy1", password: "123", loginType: "ACCOUNT"});
        buffer = LoginRequest.encode(loginRequest).finish();
        arrayBuffer = new ArrayBuffer(4 + buffer.length);

        var dataView = new DataView(arrayBuffer);
        dataView.setInt32(0, 10001);
        for (var i = 0; i < buffer.length; i++) {
            dataView.setUint8(i + 4, buffer[i]);
        }
        console.log(dataView.getInt32(0));
    });

    var wsUri = "ws://127.0.0.1:8005/";
    var output;

    function connectWebSocket() {
        output = document.getElementById("output");
        testWebSocket();
    }

    function testWebSocket() {
        websocket = new WebSocket(wsUri);
        websocket.onopen = function (evt) {
            onOpen(evt)
        };
        websocket.onclose = function (evt) {
            onClose(evt)
        };
        websocket.onmessage = function (evt) {
            onMessage(evt)
        };
        websocket.onerror = function (evt) {
            onError(evt)
        };
    }

    function onOpen(evt) {
        writeToScreen("CONNECTED");
        doSend(arrayBuffer);
    }

    function onClose(evt) {
        writeToScreen("DISCONNECTED");
    }

    function onMessage(evt) {
        var reader = new FileReader();
        reader.readAsArrayBuffer(evt.data);
        reader.onload = function (e) {
            var dataView=new DataView(reader.result,0,4);
            var mid= dataView.getInt32(0);
            var buf = new Uint8Array(reader.result,4);
            writeToScreen('收到消息：'+mid);
            var response= LoginResponse.decode(buf);
            console.log(response);
            writeToScreen('<span style="color: blue;">RESPONSE: ' +response.rid + '</span>');
        }
    }

    function onError(evt) {
        writeToScreen('<span style="color: red;">ERROR:</span> ' + evt.data);
    }

    function doSend(message) {
        if (loginRequest != null) {
            writeToScreen("SENT: " + loginRequest.account);
        }
        websocket.send(message);
    }

    //显示到页面上
    function writeToScreen(message) {
        var pre = document.createElement("p");
        pre.style.wordWrap = "break-word";
        pre.innerHTML = message;
        output.appendChild(pre);
    }

    //	        window.addEventListener("load", init, false);

</script>
</body>
</html>